package com.movie.playvideos.common;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.movie.playvideos.objects.MediaItemObject;
import com.movie.playvideos.objects.MediaObject;

import android.util.Log;


public class DecoderJson {
	public static String getTokenFromData (String data){
		String result = "";
		try {
			JSONObject json = new JSONObject(data);
			JSONObject jsonData = json.getJSONObject("data");
			String token = jsonData.getString("Token");
			result = token;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static MediaObject getListMedia (String data){
		MediaObject media = new MediaObject();
		ArrayList<MediaItemObject> result = new ArrayList<MediaItemObject>();
		
		try {
			Log.e("Data: ", data);
			JSONObject json = new JSONObject(data);
			JSONObject jsonData = null;
			if (json.has("data")) {
				jsonData = json.getJSONObject("data");	
			}
			
			if (jsonData != null) {
				if (jsonData.has("MediaServer")) {
					media.setMediaServer(jsonData.getString("MediaServer"));
					if (jsonData.has("Data")) {
						JSONArray arrayData = jsonData.getJSONArray("Data");
						
						for(int i = 0; i < arrayData.length(); i++){
							JSONObject jsonMedia = arrayData.getJSONObject(i);
							MediaItemObject mediaObject = new MediaItemObject();
							mediaObject.setId(jsonMedia.has("ID") == true ?  jsonMedia.getString("ID") :  "");
							mediaObject.setName(jsonMedia.has("Name") == true ?  jsonMedia.getString("Name") :  "");
							mediaObject.setDes(jsonMedia.has("Description") == true ?  jsonMedia.getString("Description") :  "");
							mediaObject.setPlayUrl(jsonMedia.has("PlayUrl") == true ?  jsonMedia.getString("PlayUrl") :  "");
							mediaObject.setRestUrl(jsonMedia.has("RestUrl") == true ?  jsonMedia.getString("RestUrl") :  "");
							mediaObject.setSecretCode(jsonMedia.has("SecretCode") == true ?  jsonMedia.getString("SecretCode") :  "");
							mediaObject.setPackageId(jsonMedia.has("PackageID") == true ?  jsonMedia.getString("PackageID") :  "");
							mediaObject.setPackageName(jsonMedia.has("PackageName") == true ?  jsonMedia.getString("PackageName") :  "");
							mediaObject.setPackageImage(jsonMedia.has("PackageImage") == true ?  jsonMedia.getString("PackageImage") :  "");
							mediaObject.setImg(jsonMedia.has("Image") == true ?  jsonMedia.getString("Image") :  "");
							mediaObject.setTtl(jsonMedia.getString("TTL"));
							
							result.add(mediaObject);
						}
						
						media.setListMedia(result);
					}
					
				}
			}
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return media;
	}
	public static String getStreammingUrlFromXML (String xml){
		XMLParser parser = new XMLParser();
		Document doc = parser.getDomElement(xml);
		String returnCode = parser.getAttribute((Element) doc.getDocumentElement(), "code");
		if(returnCode.equals("200")){
			NodeList nlData = doc.getElementsByTagName("data");
			Element e = (Element) nlData.item(0);
			NodeList nlChannel = e.getElementsByTagName("channel");
			String streamUrl = parser.getValue((Element) nlChannel.item(0), "stream_url");
			return streamUrl;
		}
		else return "";
	}
	
}
